﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using _1Pay;
namespace Guline.PaymentAPI
{
    public class OnePay
    {
        public String sendPost(String access_key, String secret_key, String type, String pin, String serial)
        {
            String result = "";
            String url = "https://api.1pay.vn/card-charging/v2/topup";
            _1Pay.My1Pay myonepay = new My1Pay();
            String signature = myonepay.generateSignature_Card(access_key, pin, serial, type, secret_key); //create signature
            String urlParameter ="access_key="+access_key+"&type="+type+"&pin="+pin+"&serial="+serial+"&signature="+signature;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = "Mozilla/5.0";
                WebHeaderCollection headerReader = request.Headers; headerReader.Add("Accept-Language", "en-US,en;q=0.5");
                var data = Encoding.ASCII.GetBytes(urlParameter);
                request.ContentLength = data.Length;
                Stream requestStream = request.GetRequestStream();
                // send url param
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                result = new StreamReader(response.GetResponseStream()).ReadToEnd();
                response.Close();
            }
            catch (Exception e)
            {
                result = e.GetBaseException().ToString();
            }
            return result;
        }
    
    }
}
